<?php
function ListIDs($db, $table, $where="1=1")
{
	$listIDs = "";
	$query = "SELECT GROUP_CONCAT(id SEPARATOR ',') AS ListIDs FROM $table WHERE $where";
	if ($db->query($query)) {
		while ($db->next_record()) 	{
			$listIDs = $db->f("ListIDs");
		}
	}
	return $listIDs;
}

function GeneratePartnerCode($db, $provinceid, $where="")
{
	$maxCode = "00000";
	$province = provinceDAL::Load($db, $provinceid, $pageError);
	$query = "SELECT MAX(SUBSTR(partnercode, 3, 5)) AS MaxCode FROM partner ".($where!=""?" WHERE $where":"");
	if ($db->query($query)) {
		while ($db->next_record()) 	{
			$maxCode = $db->f("MaxCode");
		}
	}
	$max = (int)$maxCode + 1;
	
	return sprintf("PA%05d".$province->provincecode, $max);
}

function GenerateCateCode($db)
{
	$maxCode = "00000";
	$query = "SELECT MAX(SUBSTR(catecode, 2, 5)) AS MaxCode FROM categories WHERE CHAR_LENGTH(catecode)=6";
	if ($db->query($query)) {
		while ($db->next_record()) 	{
			$maxCode = $db->f("MaxCode");
		}
	}
	$max = (int)$maxCode + 1;
	
	return sprintf("C%05d", $max);
}

function GenerateSubCateCode($db)
{
	$maxCode = "00000";
	$query = "SELECT MAX(SUBSTR(catechildcode, 3, 5)) AS MaxCode FROM categorieschild WHERE CHAR_LENGTH(catechildcode)=7";
	if ($db->query($query)) {
		while ($db->next_record()) 	{
			$maxCode = $db->f("MaxCode");
		}
	}
	$max = (int)$maxCode + 1;
	
	return sprintf("SC%05d", $max);
}

function GenerateProductCode($db)
{
	$maxCode = "00000";
	$query = "SELECT MAX(SUBSTR(procode, 2, 5)) AS MaxCode FROM products WHERE CHAR_LENGTH(procode)=6";
	if ($db->query($query)) {
		while ($db->next_record()) 	{
			$maxCode = $db->f("MaxCode");
		}
	}
	$max = (int)$maxCode + 1;
	
	return sprintf("S%05d", $max);
}
?>